package cn.iocoder.yudao.module.ai.controller.admin.creationwork;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ai.controller.admin.creationwork.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.creationwork.CreationWorkDO;
import cn.iocoder.yudao.module.ai.service.creationwork.CreationWorkService;

@Tag(name = "管理后台 - AI 创作作品")
@RestController
@RequestMapping("/ai/creation-work")
@Validated
public class CreationWorkController {

    @Resource
    private CreationWorkService creationWorkService;

    @PostMapping("/create")
    @Operation(summary = "创建AI 创作作品")
    @PreAuthorize("@ss.hasPermission('ai:creation-work:create')")
    public CommonResult<Long> createCreationWork(@Valid @RequestBody CreationWorkSaveReqVO createReqVO) {
        return success(creationWorkService.createCreationWork(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI 创作作品")
    @PreAuthorize("@ss.hasPermission('ai:creation-work:update')")
    public CommonResult<Boolean> updateCreationWork(@Valid @RequestBody CreationWorkSaveReqVO updateReqVO) {
        creationWorkService.updateCreationWork(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI 创作作品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:creation-work:delete')")
    public CommonResult<Boolean> deleteCreationWork(@RequestParam("id") Long id) {
        creationWorkService.deleteCreationWork(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI 创作作品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:creation-work:query')")
    public CommonResult<CreationWorkRespVO> getCreationWork(@RequestParam("id") Long id) {
        CreationWorkDO creationWork = creationWorkService.getCreationWork(id);
        return success(BeanUtils.toBean(creationWork, CreationWorkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI 创作作品分页")
    @PreAuthorize("@ss.hasPermission('ai:creation-work:query')")
    public CommonResult<PageResult<CreationWorkRespVO>> getCreationWorkPage(@Valid CreationWorkPageReqVO pageReqVO) {
        PageResult<CreationWorkDO> pageResult = creationWorkService.getCreationWorkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CreationWorkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI 创作作品 Excel")
    @PreAuthorize("@ss.hasPermission('ai:creation-work:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCreationWorkExcel(@Valid CreationWorkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CreationWorkDO> list = creationWorkService.getCreationWorkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI 创作作品.xls", "数据", CreationWorkRespVO.class,
                        BeanUtils.toBean(list, CreationWorkRespVO.class));
    }

}